package org.share.comm.compress;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * zip压缩文件工具类
 * 
 * @author hhm
 *
 */
public class ZipUtils {

	/**
	 * 遍历压缩文件中文件，回调处理文件
	 * 
	 * @param zipFile
	 *            压缩文件
	 * @param callback
	 *            遍历回调方法
	 * @throws Exception
	 */
	public static void readFile(File zipFile, ZipUtils.ZipReadFileCallback callback) throws Exception {
		ZipFile zip;
		zip = new ZipFile(zipFile);
		Enumeration<? extends ZipEntry> enumeration = zip.entries();
		ZipEntry zipEntry = null;
		while (enumeration.hasMoreElements()) {
			zipEntry = enumeration.nextElement();
			boolean stop = callback.deal(zipEntry, zip);
			if (stop) {
				// 终止遍历
				break;
			}
		}
	}

	/**
	 * 遍历zip文件回调接口
	 * 
	 * @author hhm
	 *
	 */
	public interface ZipReadFileCallback {
		/**
		 * 压缩文件中文件实体回调方法
		 * 
		 * @param zipEntry
		 *            压缩文件中实体
		 * @param zipFile
		 *            压缩源文件
		 * @return true 停止遍历 false继续遍历
		 */
		boolean deal(ZipEntry zipEntry, ZipFile zipFile);
	}

	/**
	 * 使用zip方式对字符串进行压缩
	 * 
	 * @param str
	 *            待压缩的字符串
	 * @return 压缩后字节流
	 */
	public static byte[] zipString(String str) {
		if (str == null || str.equals("")) {
			return null;
		}
		byte[] rs = null;
		ByteArrayOutputStream out = null;
		ZipOutputStream zipOutputStream = null;
		try {
			out = new ByteArrayOutputStream();
			zipOutputStream = new ZipOutputStream(out);
			zipOutputStream.putNextEntry(new ZipEntry("0"));
			zipOutputStream.write(str.getBytes());
			zipOutputStream.closeEntry();
			rs = out.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (zipOutputStream != null) {
					zipOutputStream.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return rs;
	}

	/**
	 * 使用zip方式对字符串进行解压缩
	 * 
	 * @param compressed
	 *            压缩字节流
	 * @return 解压缩后的字符串
	 */
	public static String unzipString(byte[] compressed) {
		if (compressed == null) {
			return null;
		}
		ByteArrayOutputStream out = null;
		ByteArrayInputStream in = null;
		ZipInputStream zipInputStream = null;
		String decompressed = null;
		try {
			out = new ByteArrayOutputStream();
			in = new ByteArrayInputStream(compressed);
			zipInputStream = new ZipInputStream(in);
			zipInputStream.getNextEntry();
			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = zipInputStream.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
			decompressed = out.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (zipInputStream != null) {
					zipInputStream.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return decompressed;
	}

}
